/**
 * Created by vision on 16/2/29.
 */
module.exports = function($rootScope, $q,$location,$cookies){
    return {

        request:function(config){

            var token = $cookies.get("cookieToken");

            if (token != null && token != '' && token != undefined) {
                config.headers.token = token;
            }
            return config;
        },
        response: function (response) {
          if (response.data.code == 0) {
            if(!$rootScope.errorMessage.show) {
              var message = response.data.message || response.data.msg;
              if(message != undefined && message != null && message != "") {
                $('#globalErrorMessage').modal({
                  backdrop: 'static',
                  show: true,
                  keyboard: false
                });
                // $rootScope.errorMessage.show = true;
                $rootScope.errorMessage.content = message;
              }

            }
            return response;
          }
          /*if (response.data.code === 2) {
            //未补全信息
            $location.path('/workBar/registerSuccess');
            return $q.reject(response);
          }*/
          //session过期，重新登陆
          if (response.data.code === 3) {
            $location.path('/login');
            return $q.reject(response);
          }
          return response;
        },
        responseError: function(response) {
          // console.log("返回参数", response.data.code);

            if(response.status == 401 || response.status == 403){
                $location.path('/login');
                $cookies.remove('userIdBenDiFilter');
            }
            return $q.reject(response);
        },

        // responseError: function (response) {
        //     $rootScope.$broadcast({
        //         //401 Unauthorized — The user is not logged in
        //         401: AUTH_EVENTS.notAuthenticated,
        //         //403 Forbidden — The user is logged in but isn’t allowed access
        //         403: AUTH_EVENTS.notAuthorized,
        //         //419 Authentication Timeout (non standard) — Session has expired
        //         419: AUTH_EVENTS.sessionTimeout,
        //         //440 Login Timeout (Microsoft only) — Session has expired
        //         440: AUTH_EVENTS.sessionTimeout
        //     }[response.status], response);
        //     return $q.reject(response);
        // }
    };
};


